package com.hda.adbt;

import java.awt.Point;
import java.util.Vector;

public class MinimalBoundingRectangle 
{
	Point upperRight = new Point(), lowerLeft = new Point();
	
	// Complexity O(n) - Only one loop here.
	public boolean createMBR(Vector<Point> p)
	{
		int minX = -9999, maxX = -9999, minY = -9999, maxY = -9999;
		
		for(int i=0; i < p.size(); i++)
		{
			if( i == 0 )
			{
				minX = p.elementAt(i).x;
				maxX = p.elementAt(i).x;
				minY = p.elementAt(i).y;
				maxY = p.elementAt(i).y;
			}
			
			if(p.elementAt(i).x < minX ) minX = p.elementAt(i).x;
			else if(p.elementAt(i).x > maxX) maxX = p.elementAt(i).x;
			
			if(p.elementAt(i).y < minY ) minY = p.elementAt(i).y;
			else if(p.elementAt(i).y > maxY) maxY = p.elementAt(i).y;
		}
		
		lowerLeft.x = minX;
		lowerLeft.y = minY;
		upperRight.x = maxX;
		upperRight.y = maxY;
		return true;
	}
	
	public void printMBR()
	{
		System.out.println("lowerLeft: "+lowerLeft+"\nupperRight: "+upperRight);
	}
}
